********************************************************************************
* Panel A: Elections where winner/runner-up was a prosecutor
********************************************************************************

use "dataset_elections", clear

replace runnerup = "(No challenger)" if runnerup == "NA"
replace runnerup = "Runner-up has not prosecuted" if runnerup == "no"
replace runnerup = "Runner-up has prosecuted" if runnerup == "yes"
replace winnerh = "Winner has not prosecuted" if winnerh == "no"
replace winnerh = "Winner has prosecuted" if winnerh == "yes"

estpost tabulate winnerhas runneruphas

#delimit;

esttab
	using "_output/table 1a.tex"
	,
		replace
		booktabs
		unstack 
		cells(b pct(par fmt(0)))
		compress
		noobs
		nonum
		collabels(none)
		nomtitles
		gap
		;
		
#delimit cr

********************************************************************************
* Panel B: Incumbents that worked in the office
********************************************************************************

use "dataset_district_attorneys", clear

replace worked = "(Unknown)" if worked == "NA"
replace worked = "Worked in office" if worked == "yes"
replace worked = "Did not work in office" if worked == "no"

label variable worked "Worked"

eststo clear

estpost tabulate worked

#delimit;

esttab
	using "_output/table 1b.tex"
	,
		replace
		booktabs
		cells("b pct(par fmt(0))") 
		noobs 
		label
		nomtitles
		collabels("Number" "Percent")
		nonum
		width(0.5\linewidth)
		gap
		;

#delimit cr

********************************************************************************
* Panel C: Competition
********************************************************************************

use dataset_legislative_elections, clear

append using dataset_elections

collapse (count) ntotal=incran ///
	(sum) incrann=incran incoppon=incoppo incwonn=incwon ///
	incoppwonn=incoppwon ///
	(mean) incranm=incran incoppom=incoppo incwonm=incwon ///
	incoppwonm=incoppwon, by(office)

reshape i office
reshape j stat n m p
reshape xi ntotal
reshape xij incran incoppo incoppwon
reshape long

foreach x in incran incoppo incoppwon {
	replace `x' = round(`x', 0.01)*100 if stat == "m"
	tostring `x', replace force
	replace `x' = "(" + `x' + ")" if stat == "m"
	replace `x' = "" if `x' == "."
}
replace stat = "l" if stat == "n"
sort office stat

replace office = office[_n-1] if mi(office)
replace office = "" if stat != "l"

tostring ntotal, replace
replace ntotal = "" if stat != "l"
drop stat
replace office = "" if ntotal == ""

replace office = "District attorney" if office == "DA"
replace office = "State assembly" if office == "SA"
replace office = "State senate" if office == "SS"
replace office = "US House" if office == "UH"

drop if _n == 12

#delimit;

listtex 
	*
		using "_output/table 1c.tex"
		,
		replace
		type rstyle(tabular)
		head(
			\begin{tabular*}{0.7\linewidth}{
				@{\hskip\tabcolsep\extracolsep\fill}l*{5}{l}}
			\toprule Office & Total & Incumbent & Incumbent 
				& Incumbent \\
			& races & ran  & opposed  & 
				won if opposed \\
			\midrule
		)
		foot("\bottomrule\end{tabular*}")
		;
		
#delimit cr

********************************************************************************
* End
********************************************************************************
